/*---------------------------------------------------------
Task: PlaySequence
data.ID 	- sequence id
data.Name 	- sequence name (Must provide either id or name)
data.Wait	- Optional. Should we wait for sequence to finish
data.Speed	- Optional. Playback speed of sequence   
---------------------------------------------------------*/
function ENT:TaskStart_PlaySequence(data)
	local SequenceID = data.ID
	if (data.Name) then SequenceID = self:LookupSequence(data.Name)	end
	self:ResetSequence(SequenceID)
	self:SetNPCState(NPC_STATE_SCRIPT)
	local Duration = self:SequenceDuration()
	if (data.Speed && data.Speed > 0) then 
		SequenceID = self:SetPlaybackRate(data.Speed)
		Duration = Duration / data.Speed
	end
	self.TaskSequenceEnd = CurTime() + Duration
end
function ENT:Task_PlaySequence(data)
	// Wait until sequence is finished
	if (CurTime() < self.TaskSequenceEnd) then return end
	self:TaskComplete()
	self:SetNPCState(NPC_STATE_NONE)
	// Clean up
	self.TaskSequenceEnd = nil
end

/*---------------------------------------------------------
Task: FindEnemy
data.Class  - class of enemy entity
data.Radius - radius to find enemies in
---------------------------------------------------------*/
function ENT:TaskStart_FindEnemy(data)
	local et =  ents.FindInSphere(self:GetPos(),data.Radius or 512)
	for k,v in ipairs(et) do
		if ( v:IsValid() && v != self && v:GetClass() == data.Class ) then
			print("ENEMY SET")
			self:SetEnemy(v,true)
			self:UpdateEnemyMemory(v,v:GetPos())
			self:TaskComplete()
			return
		end
	
	end
	self:SetEnemy(NULL)	
end
function ENT:Task_FindEnemy(data)
end

/*---------------------------------------------------------
Task: AttackEnemy
---------------------------------------------------------*/
function ENT:TaskStart_AttackEnemy(data)
	local SequenceID = choose("attackA","attackB","attackC","attackD","attackE","attackF")
	if (data.Name) then SequenceID = self:LookupSequence(data.Name)	end
	self:ResetSequence(SequenceID)
	self:SetNPCState(NPC_STATE_SCRIPT)
	local Duration = self:SequenceDuration()
	if (data.Speed && data.Speed > 0) then 
		SequenceID = self:SetPlaybackRate(data.Speed)
		Duration = Duration / data.Speed
	end
	self.TaskSequenceEnd = CurTime() + Duration
end
function ENT:Task_AttackEnemy(data)
	// Wait until sequence is finished
	if (CurTime() < self.TaskSequenceEnd) then return end
	self:TaskComplete()
	self:SetNPCState(NPC_STATE_NONE)
	// Clean up
	self.TaskSequenceEnd = nil
end

function choose(...)
	return arg[math.random(0,#arg)]
end
